<?php
namespace app\bbs\controller;
class User extends Base
{
    public function __construct(){
        parent::__construct();
        if($this->a!='index')
            $this->checkLogin();
    }

    //用户首页
    public function index(){
        $id = $this->get('id');
        $user = $this->db->find('user',$id);
        $tie  = $this->db->query('select * from tie where user_id=? order by id desc',[$id]);
        $reply= $this->db->query('select a.*,b.title as tie_title from reply a left join tie b on a.tie_id=b.id where a.user_id=? order by a.id desc',[$id]);
        $d = [
            'title'=> '用户主页',
            'user' => $user,
            'tie'  => $tie,
            'reply'=> $reply,
        ];
        $this->fetch($d);
    }

    //基本设置
    public function set(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'username' => "require|length:2,30|unique:user,username,{$this->userinfo['id']}",
                'email' => "email|unique:user,email,{$this->userinfo['id']}"
            ];
            $msg = [
                'username.requier' => '用户名不能为空',
                'username.unique' => '用户名已经被注册',
                'username.length' => '用户名在6-20个字符之间',
                'email' => '邮箱格式错误',
                'email.unique' => '邮箱已经被注册过',
            ];
            check($rule,$msg,$d);
            $d['id'] = $this->userinfo['id'];
            $this->db->update('user',$d);
            cookie('js_msg','操作成功');
            suc();
        }
        $d = [
            'title' => '基本设置',
            'userinfo' => $this->db->find('user',$this->userinfo['id']),
        ];
        return $this->fetch($d);
    }

    //上传头像
    public function avatar(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'pic'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            if(isset($d['file'])) unset($d['file']);
            $d['id'] = $this->userinfo['id'];
            $this->db->update('user',$d);
            m('Attachment','xcom')->imgSure('user',$d['id'],$d);
            cookie('js_msg','修改头像成功');
            suc();
        }
    }

    //更改密码
    public function pwd(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'password'=>'require|min:6',
                'repassword'=>'confirm:password',
            ];
            $msg = [
                'password.require' => '请输入密码',
                'password.min' => '密码至少6位',
                'repassword.confirm' => '密码不一致',
            ];
            check($rule,$msg,$d);
            $d['password'] = com_password($d['password']);
            $d['id'] = $this->userinfo['id'];
            $this->db->update('user',$d);
            cookie('js_msg','修改密码成功');
            suc();
        }
    }

    //我的发帖
    public function tie(){
        $page = $this->get('page',1);
        $limit = $this->get('limit',10);
        $sql = "
            select
                a.*,b.is_rz,b.pic,b.username,b.level,c.cat_title
            from
                tie a
            left join
                user b
            on
                a.user_id=b.id
            left join
                tie_cat c
            on
                a.cat_id=c.id
            where
                [1]
            order by reply_time desc,update_time desc
        ".limit($page,$limit);
        $w = " a.user_id=? ";
        $param = [$this->userinfo['id']];
        $sql = str_replace("[1]", $w, $sql);
        $rs =  $this->db->query($sql, $param,'',true);
        $d = [
            'title'=> '我的发帖',
            'list' => $rs[0],
            'page_count' => $rs[1],
            'page' => $page,
        ];
        $this->fetch($d);
    }

    //淘宝客
    public function tbk(){
      $this->fetch();
    }
}
